<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>档案属性管理</title>
    <link href="/component/pear/css/pear.css" rel="stylesheet"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">档案属性名:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileAttributeName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="attribute-query">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="role-table" lay-filter="role-table"></table>
    </div>
</div>

<script type="text/html" id="role-toolbar">
    <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">
        <i class="layui-icon layui-icon-add-1"></i>
        新增
    </button>
<!--    <button class="pear-btn pear-btn-danger pear-btn-md" lay-event="batchRemove">-->
<!--        <i class="layui-icon layui-icon-delete"></i>-->
<!--        删除-->
<!--    </button>-->
</script>

<script type="text/html" id="role-bar">
    <button class="pear-btn pear-btn-primary pear-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>
    </button>
    <!--			<button class="pear-btn pear-btn-warming pear-btn-sm" lay-event="power"><i class="layui-icon layui-icon-vercode"></i></button>-->
    <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i class="layui-icon layui-icon-delete"></i>
    </button>
</script>

<script type="text/html" id="role-enable">
    <input type="checkbox" name="enable" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="role-enable" {{
           d.enable== true ? 'checked' : '' }} />
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let cols = [
            [
                {title: '档案属性名称', field: 'file_attribute_name', align: 'center', width: 120},
                {title: '创建时间', field: 'create_time', align: 'center'},
                {title: '更新时间', field: 'update_time', align: 'center'},
                {title: '操作', toolbar: '#role-bar', align: 'center', width: 130}
            ]
        ]

        tableIns = table.render({
            elem: '#role-table',
            url: '/file/attribute/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: '#role-toolbar',
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: 'testReload',
            //解决页码的问题
            done: function (res) {
                // 获得当前页码
                var brforeCurr = tableIns.config.page.curr;
                // 获得当前页的记录数
                var dataLength = table.cache['testReload'].length;
                var count = res.count; // 获得总记录数
                //如果当前页的记录数为0并且总记录数不为0
                if (dataLength == 0 && count != 0) {
                    // 刷新表格到上一页
                    table.reload("testReload", {
                        page: {
                            curr: brforeCurr - 1
                        }
                    });
                }
            }
        })


        table.on('tool(role-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            } else if (obj.event === 'edit') {
                window.edit(obj);
            }
        });

        table.on('toolbar(role-table)', function (obj) {
            if (obj.event === 'add') {
                window.add();
            } else if (obj.event === 'refresh') {
                window.refresh();
            }
        });

        form.on('submit(attribute-query)', function (data) {
            table.reload('testReload', {where: data.field})
            return false;
        });

        // 新增角色
        window.add = function () {
            layer.open({
                type: 2,
                title: '新增档案属性',
                shade: 0.1,
                area: ['500px', '400px'],
                content: "/file/attribute/add"
            });
        }
        //修改角色
        window.edit = function (obj) {
            var data = obj.data;
            layer.open({
                type: 2,
                title: '修改档案属性',
                shade: 0.1,
                area: ['500px', '400px'],
                content: '/file/attribute/edit?attributeId=' + data.id
            });
        }
        // 单行删除功能
        window.remove = function (obj) {
            layer.confirm('确定要删除该文档属性', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/file/attribute/remove/" + obj.data['id'],
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 3000}, function () {
                                obj.del();
                                table.reload("testReload");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 3000});
                        }
                    }
                })
            });
        }
        // 批量删除
        window.batchRemove = function (obj) {
            let data = table.checkStatus(obj.config.id).data;
            if (data.length === 0) {
                layer.msg("未选中数据", {icon: 3, time: 2000});
                return false;
            }
            let ids = "";
            for (let i = 0; i < data.length; i++) {
                ids += data[i].role_id + ",";
            }
            ids = ids.substr(0, ids.length - 1);
            layer.confirm('确定要删除这些用户', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/api/role/remove/" + ids,
                    dataType: 'json',
                    type: 'delete',
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {icon: 1, time: 2000}, function () {
                                // 	table.reload('testReload', {//testReload是组件名称,表格名
                                // 		page: {//page参数是指重新刷新后从第几页开始
                                // 			curr: 1 //重新从第 1 页开始
                                // 		}
                                // 		,where: {//where是指你要往后台发的参数的键值对,可以不加key{}
                                // 		}
                                // 	});//data是默认值

                                table.reload("testReload");
                            });
                        } else {
                            layer.msg(result.message, {icon: 2, time: 2000});
                        }
                    }
                })
            });
        }
        // 刷新按钮
        window.refresh = function () {
            table.reload('testReload');
        }
    })
</script>
</body>
</html>
